<script setup lang="ts">
  import { ref, watch, reactive, computed } from 'vue';
  import {
    wmsDeliverySend,
    wmsDeliveryResend,
  } from '@/api/operation-monitoring';
  import { Notification } from '@arco-design/web-vue';

  const columns: any[] = [
    {
      title: '序号',
      dataIndex: 'PAGEHELPER_ROW_ID',
      key: 'PAGEHELPER_ROW_ID',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '合格证',
      dataIndex: 'mesCheckQualiticationId',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '型号',
      dataIndex: 'mProductNumber',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '小车码',
      dataIndex: 'carCode',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '托盘码',
      dataIndex: 'toolCode',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '日计划时间',
      dataIndex: 'requiredTime',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: 'AO编码',
      dataIndex: 'aoNo',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '图号',
      dataIndex: 'itemCode',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '质量编号',
      dataIndex: 'qualityCode',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '配送数量',
      dataIndex: 'deliveryQty',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '组号',
      dataIndex: 'groupCode',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '条码号',
      dataIndex: 'barcode',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '机型',
      dataIndex: 'productCode',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '批次',
      dataIndex: 'batchNo',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '起始架次',
      dataIndex: 'startLotNo',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '截止架次',
      dataIndex: 'endLotNo',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: 'ERP交接单ID',
      dataIndex: 'erpWmsDeliveryPlanId',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '配套信息ID',
      dataIndex: 'infoId',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '交接单ID',
      dataIndex: 'wdpId',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '质量ID',
      dataIndex: 'qualityId',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
  ];
  // 创建一个响应式的本地副本
  const localTableData = ref<any>();
  // 数据库一共有多少条
  const total = ref<number>(1);
  const pageNum = ref<number>(1);
  const pageSize = ref<number>(10);
  const rangeDate = ref<Date[]>([]);
  const selectedKeys = ref<string[]>();
  const rowSelection = reactive({
    type: 'checkbox' as 'checkbox' | 'radio',
    showCheckedAll: true,
  });
  // 计算已选择的表格数据
  const selectedTableData = computed(() => {
    return localTableData.value.filter((item: any) =>
      selectedKeys.value?.includes(item.PAGEHELPER_ROW_ID)
    );
  });
  const formValues = reactive({
    palletCode: '',
    packageCode: '',
    aoCode: '',
    startPlaneNo: '',
    startTime: '',
    endTime: '',
  });
  async function handleSearch() {
    const response = await wmsDeliverySend({
      queryParam: formValues,
      page: {
        total: 0,
        pageSize: pageSize.value,
        currentPage: pageNum.value,
      },
    });
    if (response.list) {
      localTableData.value = response.list;
      total.value = response.total;
    } else {
      Notification.warning({
        title: '提示',
        content: '查询失败',
        closable: true,
      });
    }
  }
  function reset() {
    Object.keys(formValues).forEach((key) => {
      (formValues as Record<string, any>)[key] = '';
    });
    rangeDate.value = [];
    setTimeout(() => {
      handleSearch();
    }, 150);
  }
  async function redelivery() {
    if (selectedKeys.value?.length === 0) {
      Notification.warning({
        title: '提示',
        content: '请选择至少一条数据',
        closable: true,
      });
    } else {
      const promises = selectedTableData.value.map(async (data: any) => {
        const response = await wmsDeliveryResend(data);
        const id = data.PAGEHELPER_ROW_ID;
        if (response.retCode === '200') {
          Notification.success({
            title: '提示',
            content: ` 序号 ${id}${response.message} `,
            closable: true,
          });
        } else {
          Notification.success({
            title: '失败',
            content: ` 序号 ${id}${
              response.message ? response.message : '请求失败,请重新尝试'
            }`,
            closable: true,
          });
        }
      });
      await Promise.all(promises);
      selectedKeys.value = [];
    }
  }
  // 监听页面变化
  watch([pageNum, pageSize], () => {
    handleSearch();
  });
  watch(rangeDate, () => {
    if (rangeDate.value && rangeDate.value.length >= 2) {
      formValues.startTime = rangeDate.value[0].toString();
      formValues.endTime = rangeDate.value[1].toString();
    }
  });
  // 监听已选择数据的序号，并更新到已选择表格数据行字段
  function change(e: any) {
    pageNum.value = e;
  }
  function changeSize(e: any) {
    pageSize.value = e;
  }
</script>

<template>
  <div class="content-page">
    <a-row :gutter="{ md: 8, lg: 24, xl: 32 }">
      <a-col :span="6">
        <a-input
          v-model="formValues.palletCode"
          placeholder="托盘编码"
          size="large"
          @press-enter="handleSearch"
        />
      </a-col>
      <a-col :span="6">
        <a-input
          v-model="formValues.packageCode"
          placeholder="工作包编码"
          size="large"
          @press-enter="handleSearch"
        />
      </a-col>
      <a-col :span="6">
        <a-input
          v-model="formValues.aoCode"
          placeholder="AO编码"
          size="large"
          @press-enter="handleSearch"
        />
      </a-col>
      <a-col :span="6">
        <a-input
          v-model="formValues.startPlaneNo"
          placeholder="架次"
          size="large"
          @press-enter="handleSearch"
        />
      </a-col>
      <a-col :span="12">
        <a-range-picker v-model:model-value="rangeDate" style="width: auto" />
      </a-col>
    </a-row>
    <a-space style="margin-top: 16px">
      <a-button type="primary" size="large" @click="handleSearch">
        查询
      </a-button>
      <a-button size="large" @click="reset">重置</a-button>
      <a-button type="outline" size="large" @click="redelivery">
        重新配送
      </a-button>
    </a-space>
    <a-divider />
    <div>
      <a-table
        v-model:selected-keys="selectedKeys"
        :stripe="true"
        class="top-table"
        :pagination="false"
        :columns="columns"
        :data="localTableData"
        :row-selection="rowSelection"
        row-key="PAGEHELPER_ROW_ID"
      >
      </a-table>
      <div class="page-position">
        <a-pagination
          :total="total"
          :current="pageNum"
          :page-size="pageSize"
          show-total
          show-jumper
          show-page-size
          @change="change"
          @update:page-size="changeSize"
        />
      </div>
    </div>
  </div>
</template>

<style scoped>
  span {
    margin-bottom: 16px;
  }
  .top-table {
    white-space: nowrap;
  }
  .page-position {
    margin-top: 20px;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .content-page {
    padding-left: 12px;
    padding-right: 12px;
  }
</style>
